﻿using System;
using System.Data.SqlTypes;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.Base
{
#pragma warning disable 8500

    /// <summary>
    /// Convert类型扩展方法类
    /// </summary>
    unsafe public static partial class ConvertEx
    {
        /***************************************************************
         *      |_____________________________________________|
         *      低地址                                  高地址
         * 
         * 小端 低位字节                                高位字节
         * 大端 高位字节                                低位字节                                 
         * 
         ***************************************************************/

        private const int _BIT_LEN8 = 8;
        private const int _BIT_LEN16 = 16;
        private const int _BIT_LEN32 = 32;
        private const int _BIT_LEN64 = 64;
        private const int _BIT_LEN128 = 128;
        private const int _BIT_LEN_BIG = 256;

        private const int _BIT_INT32_AND = 1;
        private const int _BIT_INT64_AND = 1;

        #region sbyte

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] SbyteToBits(sbyte value)
        {
            byte[] bits = new byte[_BIT_LEN8];
            fixed (byte* ptr = bits)
            {
                PrimitiveSbyteToBits(value, ptr);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void SbyteToBits(sbyte value, byte[] bits, int startIndex)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN8)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN8}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveSbyteToBits(value, ptr + startIndex);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void SbyteToBits(sbyte value, Span<byte> bits)
        {
            if (bits.Length < _BIT_LEN8)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN8}");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveSbyteToBits(value, ptr);
        }

        private static void PrimitiveSbyteToBits(sbyte value, byte* ptr)
        {
            int index = _BIT_LEN8 - 1;
            for (int i = 0; i < _BIT_LEN8; i++)
            {
                ptr[index] = (byte)(value >> i & _BIT_INT32_AND);
                index--;
            }
        }






        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static sbyte BitsToSbyte(byte[] bits, int startIndex)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToSByte(ptr + startIndex, Math.Min(modLen, _BIT_LEN8));
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <returns>转换结果</returns>
        public static sbyte BitsToSbyte(Span<byte> bits)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToSByte(ptr, Math.Min(_BIT_LEN8, bits.Length));
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <returns>转换结果</returns>
        public static sbyte BitsToSbyte(SpanZ<byte> bits)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToSByte(bits.DataPtr, Math.Min(bits.Length, _BIT_LEN8));
        }

        private static sbyte PrimitiveBitsToSByte(byte* bitsPtr, int bits)
        {
            int sbyteValue = 0;
            int index = bits - 1;
            for (int i = 0; i < bits; i++)
            {
                sbyteValue |= (bitsPtr[index] << i);
                index--;
            }
            return (sbyte)sbyteValue;
        }
        #endregion

        #region byte

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] ByteToBits(byte value)
        {
            byte[] bits = new byte[_BIT_LEN8];
            fixed (byte* ptr = bits)
            {
                PrimitiveByteToBits(value, ptr);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void ByteToBits(byte value, byte[] bits, int startIndex)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN8)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN8}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveByteToBits(value, ptr + startIndex);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void ByteToBits(byte value, Span<byte> bits)
        {
            if (bits.Length < _BIT_LEN8)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
            
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveByteToBits(value, ptr);
        }

        private static void PrimitiveByteToBits(byte value, byte* ptr)
        {
            int index = _BIT_LEN8 - 1;
            for (int i = 0; i < _BIT_LEN8; i++)
            {
                ptr[index] = (byte)(value >> i & _BIT_INT32_AND);
                index--;
            }
        }






        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static byte BitsToByte(byte[] bits, int startIndex)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数组长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToByte(ptr, Math.Min(modLen, _BIT_LEN8));
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <returns>转换结果</returns>
        public static byte BitsToByte(Span<byte> bits)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToByte(ptr, Math.Min(bits.Length, _BIT_LEN8));
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <returns>转换结果</returns>
        public static byte BitsToByte(SpanZ<byte> bits)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToByte(bits.DataPtr, Math.Min(bits.Length, _BIT_LEN8));
        }

        private static byte PrimitiveBitsToByte(byte* bitsPtr, int bits)
        {
            int sbyteValue = 0;
            int index = bits - 1;
            for (int i = 0; i < bits; i++)
            {
                sbyteValue |= (bitsPtr[index] << i);
                index--;
            }
            return (byte)sbyteValue;
        }
        #endregion



        #region common

        private static void PrimitiveInt64ToBits(long value, int bits, byte* ptr, ByteSequence cpuType)
        {
            int index = bits - 1;
            if (cpuType == ByteSequence.LittleEndian)
            {
                int len = bits / 8;
                int moveBits;
                for (int i = len - 1; i >= 0; i--)
                {
                    moveBits = i * 8;
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(value >> moveBits + j & _BIT_INT64_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bits; i++)
                {
                    ptr[index] = (byte)(value >> i & _BIT_INT64_AND);
                    index--;
                }
            }
        }

        private static long PrimitiveBitsToInt64(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            long value = 0;
            long index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                //string str = string.Join(',', new Span<byte>(bitsPtr, bits).ToArray());
                int len = bits / 8;
                int moveBits;
                index = 0;
                for (int i = 0; i < len; i++)
                {
                    moveBits = i * 8;
                    for (int j = 7; j >= 0; j--)
                    {
                        value |= (long)((long)bitsPtr[index] << moveBits + j);
                        index++;
                    }
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bits; i++)
                {
                    value |= (long)((long)bitsPtr[index] << i);
                    index--;
                }
            }

            return value;
        }

        #endregion



        #region short

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] Int16ToBits(short value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN16];
            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN16, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int16ToBits(short value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN16)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN16}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN16, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int16ToBits(short value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN16)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveInt64ToBits(value, _BIT_LEN16, ptr, cpuType);
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static short BitsToInt16(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return (short)PrimitiveBitsToInt64(ptr + startIndex, Math.Min(modLen, _BIT_LEN16), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static short BitsToInt16(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return (short)PrimitiveBitsToInt64(ptr, Math.Min(_BIT_LEN16, bits.Length), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static short BitsToInt16(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return (short)PrimitiveBitsToInt64(bits.DataPtr, Math.Min(_BIT_LEN16, bits.Length), cpuType);
        }

        #endregion

        #region ushort

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] UInt16ToBits(ushort value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN16];
            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN16, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt16ToBits(ushort value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN16)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN16}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN16, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt16ToBits(ushort value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN16)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveInt64ToBits(value, _BIT_LEN16, ptr, cpuType);
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static ushort BitsToUInt16(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return (ushort)PrimitiveBitsToInt64(ptr + startIndex, Math.Min(modLen, _BIT_LEN16), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static ushort BitsToUInt16(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return (ushort)PrimitiveBitsToInt64(ptr, Math.Min(_BIT_LEN16, bits.Length), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static ushort BitsToUInt16(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return (ushort)PrimitiveBitsToInt64(bits.DataPtr, Math.Min(_BIT_LEN16, bits.Length), cpuType);
        }

        #endregion


        #region int

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] Int32ToBits(int value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN32];
            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN32, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int32ToBits(int value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN32}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN32, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int32ToBits(short value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveInt64ToBits(value, _BIT_LEN32, ptr, cpuType);
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static int BitsToInt32(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return (int)PrimitiveBitsToInt64(ptr + startIndex, Math.Min(modLen, _BIT_LEN32), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static int BitsToInt32(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return (int)PrimitiveBitsToInt64(ptr, Math.Min(bits.Length, _BIT_LEN32), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static int BitsToInt32(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return (int)PrimitiveBitsToInt64(bits.DataPtr, Math.Min(bits.Length, _BIT_LEN32), cpuType);
        }

        #endregion

        #region uint

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] UInt32ToBits(uint value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN32];
            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN32, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt32ToBits(uint value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN32}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN32, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt32ToBits(ushort value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveInt64ToBits(value, _BIT_LEN32, ptr, cpuType);
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static uint BitsToUInt32(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException($"可用的bit位数组长度不能小于{_BIT_LEN32}");
            }

            fixed (byte* ptr = bits)
            {
                return (uint)PrimitiveBitsToInt64(ptr + startIndex, _BIT_LEN32, cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static uint BitsToUInt32(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException($"bit位数组长度不能小于{_BIT_LEN32}");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return (uint)PrimitiveBitsToInt64(ptr, _BIT_LEN32, cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static uint BitsToUInt32(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException($"bit位数组长度不能小于{_BIT_LEN32}");
            }

            return (uint)PrimitiveBitsToInt64(bits.DataPtr, _BIT_LEN32, cpuType);
        }

        #endregion


        #region long

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] Int64ToBits(long value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN64];
            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN64, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int64ToBits(long value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN64)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN64}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveInt64ToBits(value, _BIT_LEN64, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int64ToBits(long value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN64)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveInt64ToBits(value, _BIT_LEN64, ptr, cpuType);
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static long BitsToInt64(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToInt64(ptr + startIndex, Math.Min(modLen, _BIT_LEN64), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static long BitsToInt64(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToInt64(ptr, Math.Min(bits.Length, _BIT_LEN64), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static long BitsToInt64(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToInt64(bits.DataPtr, Math.Min(bits.Length, _BIT_LEN64), cpuType);
        }

        #endregion

        #region ulong

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] UInt64ToBits(ulong value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN64];
            fixed (byte* ptr = bits)
            {
                PrimitiveUInt64ToBits(value, _BIT_LEN64, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt64ToBits(ulong value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN64)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN64}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveUInt64ToBits(value, _BIT_LEN64, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt64ToBits(ulong value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN64)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveUInt64ToBits(value, _BIT_LEN64, ptr, cpuType);
        }

        private static void PrimitiveUInt64ToBits(ulong value, int bits, byte* ptr, ByteSequence cpuType)
        {
            int index = bits - 1;
            if (cpuType == ByteSequence.LittleEndian)
            {
                int len = bits / 8;
                int moveBits;
                for (int i = len - 1; i >= 0; i--)
                {
                    moveBits = i * 8;
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(value >> moveBits + j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bits; i++)
                {
                    ptr[index] = (byte)(value >> i & _BIT_INT32_AND);
                    index--;
                }
            }
        }







        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static ulong BitsToUInt64(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToUInt64(ptr + startIndex, Math.Min(modLen, _BIT_LEN64), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static ulong BitsToUInt64(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToUInt64(ptr, Math.Min(bits.Length, _BIT_LEN64), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static ulong BitsToUInt64(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToUInt64(bits.DataPtr, Math.Min(bits.Length, _BIT_LEN64), cpuType);
        }

        private static ulong PrimitiveBitsToUInt64(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            ulong value = 0;
            int index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                //string str = string.Join(',', new Span<byte>(bitsPtr, bits).ToArray());
                int len = bits / 8;
                int moveBits;
                index = 0;
                for (int i = 0; i < len; i++)
                {
                    moveBits = i * 8;
                    for (int j = 7; j >= 0; j--)
                    {
                        value |= ((ulong)bitsPtr[index] << moveBits + j);
                        index++;
                    }
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bits; i++)
                {
                    value |= ((ulong)bitsPtr[index] << i);
                    index--;
                }
            }

            return value;
        }

        #endregion



#if CORE7_P

        #region Int128

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] Int128ToBits(Int128 value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN128];
            fixed (byte* ptr = bits)
            {
                PrimitiveInt128ToBits(value, _BIT_LEN128, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int128ToBits(Int128 value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN128)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN128}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveInt128ToBits(value, _BIT_LEN128, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void Int128ToBits(Int128 value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN128)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

            byte* ptr;
#if NET4_0
            ptr = bits.DataPtr;
#else
            ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveInt128ToBits(value, _BIT_LEN128, ptr, cpuType);
        }

        private static void PrimitiveInt128ToBits(Int128 value, int bits, byte* ptr, ByteSequence cpuType)
        {
            int index = bits - 1;
            if (cpuType == ByteSequence.LittleEndian)
            {
                int len = bits / 8;
                int moveBits;
                for (int i = len - 1; i >= 0; i--)
                {
                    moveBits = i * 8;
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(value >> moveBits + j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bits; i++)
                {
                    ptr[index] = (byte)(value >> i & _BIT_INT32_AND);
                    index--;
                }
            }
        }









        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static Int128 BitsToInt128(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }


            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToInt128(ptr + startIndex, Math.Min(modLen, _BIT_LEN128), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static Int128 BitsToInt128(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            byte* ptr;
#if NET4_0
            ptr = bits.DataPtr;
#else
            ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToInt128(ptr, Math.Min(bits.Length, _BIT_LEN128), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static Int128 BitsToInt128(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToInt128(bits.DataPtr, Math.Min(bits.Length, _BIT_LEN128), cpuType);
        }

        private static Int128 PrimitiveBitsToInt128(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            Int128 value = 0;
            int index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                //string str = string.Join(',', new Span<byte>(bitsPtr, bits).ToArray());
                int len = bits / 8;
                int moveBits;
                index = 0;
                for (int i = 0; i < len; i++)
                {
                    moveBits = i * 8;
                    for (int j = 7; j >= 0; j--)
                    {
                        value |= ((Int128)bitsPtr[index] << moveBits + j);
                        index++;
                    }
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bits; i++)
                {
                    value |= ((Int128)bitsPtr[index] << i);
                    index--;
                }
            }

            return value;
        }

        #endregion

        #region UInt128

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] UInt128ToBits(UInt128 value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN128];
            fixed (byte* ptr = bits)
            {
                PrimitiveUInt128ToBits(value, _BIT_LEN128, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt128ToBits(UInt128 value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN128)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN128}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveUInt128ToBits(value, _BIT_LEN128, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt128ToBits(UInt128 value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN128)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

            byte* ptr;
#if NET4_0
            ptr = bits.DataPtr;
#else
            ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveUInt128ToBits(value, _BIT_LEN128, ptr, cpuType);
        }

        private static void PrimitiveUInt128ToBits(UInt128 value, int bits, byte* ptr, ByteSequence cpuType)
        {
            int index = bits - 1;
            if (cpuType == ByteSequence.LittleEndian)
            {
                int len = bits / 8;
                int moveBits;
                for (int i = len - 1; i >= 0; i--)
                {
                    moveBits = i * 8;
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(value >> moveBits + j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bits; i++)
                {
                    ptr[index] = (byte)(value >> i & _BIT_INT32_AND);
                    index--;
                }
            }
        }







        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static UInt128 BitsToUInt128(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToUInt128(ptr + startIndex, Math.Min(modLen, _BIT_LEN128), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static UInt128 BitsToUInt128(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            byte* ptr;
#if NET4_0
            ptr = bits.DataPtr;
#else
            ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToUInt128(ptr, Math.Min(_BIT_LEN128, bits.Length), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static UInt128 BitsToUInt128(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToUInt128(bits.DataPtr, Math.Min(_BIT_LEN128, bits.Length), cpuType);
        }

        private static UInt128 PrimitiveBitsToUInt128(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            UInt128 value = 0;
            int index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                //string str = string.Join(',', new Span<byte>(bitsPtr, bits).ToArray());
                int len = bits / 8;
                int moveBits;
                index = 0;
                for (int i = 0; i < len; i++)
                {
                    moveBits = i * 8;
                    for (int j = 7; j >= 0; j--)
                    {
                        value |= ((UInt128)bitsPtr[index] << moveBits + j);
                        index++;
                    }
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bits; i++)
                {
                    value |= ((UInt128)bitsPtr[index] << i);
                    index--;
                }
            }

            return value;
        }

        #endregion

#endif

        /*
        #region BigInteger

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] BigIntegerToBits(BigInteger value, CPUTypes cpuType)
        {
            byte[] bits = new byte[_BIT_LEN_BIG];
            fixed (byte* ptr = bits)
            {
                PrimitiveBigIntegerToBits(value, _BIT_LEN_BIG, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt64ToBits(BigInteger value, byte[] bits, int startIndex, CPUTypes cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN_BIG)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN_BIG}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveBigIntegerToBits(value, _BIT_LEN_BIG, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void UInt64ToBits(BigInteger value, Span<byte> bits, CPUTypes cpuType)
        {
            if (bits.Length < _BIT_LEN_BIG)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

            byte* ptr;
#if NET4_0
            ptr = bits.DataPtr;
#else
            ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveBigIntegerToBits(value, _BIT_LEN_BIG, ptr, cpuType);
        }

        private static void PrimitiveBigIntegerToBits(BigInteger value, int bits, byte* ptr, CPUTypes cpuType)
        {
            int index = bits - 1;
            if (cpuType == CPUTypes.LittleEndian)
            {
                int len = bits / 8;
                int moveBits;
                for (int i = len - 1; i >= 0; i--)
                {
                    moveBits = i * 8;
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(value >> moveBits + j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bits; i++)
                {
                    ptr[index] = (byte)(value >> i & _BIT_INT32_AND);
                    index--;
                }
            }
        }







        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static BigInteger BitsToBigInteger(byte[] bits, int startIndex, CPUTypes cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN_BIG)
            {
                throw new ArgumentOutOfRangeException($"可用的bit位数组长度不能小于{_BIT_LEN_BIG}");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToBigInteger(ptr + startIndex, _BIT_LEN_BIG, cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static BigInteger BitsToBigInteger(Span<byte> bits, CPUTypes cpuType)
        {
            if (bits.Length < _BIT_LEN_BIG)
            {
                throw new ArgumentOutOfRangeException($"bit位数组长度不能小于{_BIT_LEN_BIG}");
            }

            byte* ptr;
#if NET4_0
            ptr = bits.DataPtr;
#else
            ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToBigInteger(ptr, _BIT_LEN_BIG, cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static BigInteger BitsToBigInteger(SpanZ<byte> bits, CPUTypes cpuType)
        {
            if (bits.Length < _BIT_LEN_BIG)
            {
                throw new ArgumentOutOfRangeException($"bit位数组长度不能小于{_BIT_LEN_BIG}");
            }

            return PrimitiveBitsToBigInteger(bits.DataPtr, _BIT_LEN_BIG, cpuType);
        }

        private static BigInteger PrimitiveBitsToBigInteger(byte* bitsPtr, int bits, CPUTypes cpuType)
        {
            BigInteger value = 0;
            int index;
            if (cpuType == CPUTypes.LittleEndian)
            {
                //string str = string.Join(',', new Span<byte>(bitsPtr, bits).ToArray());
                int len = bits / 8;
                int moveBits;
                index = 0;
                for (int i = 0; i < len; i++)
                {
                    moveBits = i * 8;
                    for (int j = 7; j >= 0; j--)
                    {
                        value |= ((BigInteger)bitsPtr[index] << moveBits + j);
                        index++;
                    }
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bits; i++)
                {
                    value |= ((BigInteger)bitsPtr[index] << i);
                    index--;
                }
            }

            return value;
        }


        #endregion
        */

        #region float

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] FloatToBits(float value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN32];
            fixed (byte* ptr = bits)
            {
                PrimitiveFloatToBits(value, _BIT_LEN32, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void FloatToBits(float value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN32}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveFloatToBits(value, _BIT_LEN32, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void FloatToBits(float value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN32)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveFloatToBits(value, _BIT_LEN32, ptr, cpuType);
        }

        private static void PrimitiveFloatToBits(float value, int bits, byte* ptr, ByteSequence cpuType)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            int index = bits - 1;
            byte item;

            if (cpuType == ByteSequence.LittleEndian)
            {
                for (int i = bytes.Length - 1; i >= 0; i--)
                {
                    item = bytes[i];
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(item >> j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    item = bytes[i];
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(item >> j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static float BitsToFloat(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToFloat(ptr + startIndex, Math.Min(modLen, _BIT_LEN32), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static float BitsToFloat(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToFloat(ptr, Math.Min(_BIT_LEN32, bits.Length), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static float BitsToFloat(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToFloat(bits.DataPtr, Math.Min(_BIT_LEN32, bits.Length), cpuType);
        }

        private static float PrimitiveBitsToFloat(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            byte[] bytes = new byte[4];
            int item;

            long index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                index = bits - 1;
                for (int i = bytes.Length - 1; i >= 0; i--)
                {
                    item = 0;
                    for (int j = 0; j < 8; j++)
                    {
                        item |= (bitsPtr[index] << j);
                        index--;
                    }
                    bytes[i] = (byte)item;
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bytes.Length; i++)
                {
                    item = 0;
                    for (int j = 0; j < 8; j++)
                    {
                        item |= (bitsPtr[index] << j);
                        index--;
                    }
                    bytes[i] = (byte)item;
                }
            }

            return BitConverter.ToSingle(bytes, 0);
        }

        #endregion


        #region double

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] DoubleToBits(double value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN64];
            fixed (byte* ptr = bits)
            {
                PrimitiveDoubleToBits(value, _BIT_LEN64, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void DoubleToBits(double value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN64)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN64}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveDoubleToBits(value, _BIT_LEN64, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void DoubleToBits(double value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN64)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveDoubleToBits(value, _BIT_LEN64, ptr, cpuType);
        }

        private static void PrimitiveDoubleToBits(double value, int bits, byte* ptr, ByteSequence cpuType)
        {
            byte[] bytes = BitConverter.GetBytes(value);
            int index = bits - 1;
            byte item;

            if (cpuType == ByteSequence.LittleEndian)
            {
                for (int i = bytes.Length - 1; i >= 0; i--)
                {
                    item = bytes[i];
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(item >> j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
            else
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    item = bytes[i];
                    for (int j = 0; j < 8; j++)
                    {
                        ptr[index] = (byte)(item >> j & _BIT_INT32_AND);
                        index--;
                    }
                }
            }
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static double BitsToDouble(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToDouble(ptr + startIndex, Math.Min(modLen, _BIT_LEN64), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static double BitsToDouble(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToDouble(ptr, Math.Min(_BIT_LEN64, bits.Length), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static double BitsToDouble(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToDouble(bits.DataPtr, Math.Min(_BIT_LEN64, bits.Length), cpuType);
        }

        private static double PrimitiveBitsToDouble(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            byte[] bytes = new byte[sizeof(double)];
            int item;

            long index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                index = bits - 1;
                for (int i = bytes.Length - 1; i >= 0; i--)
                {
                    item = 0;
                    for (int j = 0; j < 8; j++)
                    {
                        item |= (bitsPtr[index] << j);
                        index--;
                    }
                    bytes[i] = (byte)item;
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bytes.Length; i++)
                {
                    item = 0;
                    for (int j = 0; j < 8; j++)
                    {
                        item |= (bitsPtr[index] << j);
                        index--;
                    }
                    bytes[i] = (byte)item;
                }
            }

            return BitConverter.ToDouble(bytes, 0);
        }

        #endregion


        #region decimal

        /// <summary>
        /// 将整数转换为bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte[] DecimalToBits(decimal value, ByteSequence cpuType)
        {
            byte[] bits = new byte[_BIT_LEN128];
            fixed (byte* ptr = bits)
            {
                PrimitiveDecimalToBits(value, _BIT_LEN128, ptr, cpuType);
            }
            return bits;
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void DecimalToBits(decimal value, byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            if (bits.Length - startIndex < _BIT_LEN128)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"可用的bit位数组长度不能小于{_BIT_LEN128}");
            }

            fixed (byte* ptr = bits)
            {
                PrimitiveDecimalToBits(value, _BIT_LEN128, ptr + startIndex, cpuType);
            }
        }

        /// <summary>
        /// 将整数转换到指定的bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="value">要转换为bit位的原数据值</param>
        /// <param name="bits">转换结果bit位数组</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <exception cref="ArgumentNullException">转换结果bit位数组空引用异常</exception>
        /// <exception cref="ArgumentOutOfRangeException">转换结果bit位数组长度小于数据大小异常</exception>
        public static void DecimalToBits(decimal value, Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < _BIT_LEN128)
            {
                throw new ArgumentOutOfRangeException(nameof(bits), $"数据类型大小超过结果数组长度");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            PrimitiveDecimalToBits(value, _BIT_LEN128, ptr, cpuType);
        }

        private static void PrimitiveDecimalToBits(decimal value, int bits, byte* ptr, ByteSequence cpuType)
        {
            using (var ms = new MemoryStream())
            {
                var writer = new BinaryWriter(ms);
                writer.Write(value);
                ms.Flush();
                byte[] bytes = ms.ToArray();
                int index = bits - 1;
                byte item;

                if (cpuType == ByteSequence.LittleEndian)
                {
                    for (int i = bytes.Length - 1; i >= 0; i--)
                    {
                        item = bytes[i];
                        for (int j = 0; j < 8; j++)
                        {
                            ptr[index] = (byte)(item >> j & _BIT_INT32_AND);
                            index--;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        item = bytes[i];
                        for (int j = 0; j < 8; j++)
                        {
                            ptr[index] = (byte)(item >> j & _BIT_INT32_AND);
                            index--;
                        }
                    }
                }
            }
        }








        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="startIndex">起始索引</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        /// <exception cref="ArgumentNullException">bit位数组为null异常</exception>
        public static decimal BitsToDecimal(byte[] bits, int startIndex, ByteSequence cpuType)
        {
            if (bits == null)
            {
                throw new ArgumentNullException(nameof(bits));
            }

            int modLen = bits.Length - startIndex;
            if (modLen < 1)
            {
                throw new ArgumentOutOfRangeException("可用的bit位数据长度不能小于1");
            }

            fixed (byte* ptr = bits)
            {
                return PrimitiveBitsToDecimal(ptr + startIndex, Math.Min(modLen, _BIT_LEN128), cpuType);
            }
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static decimal BitsToDecimal(Span<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

#if NET4_0
            byte* ptr = bits.DataPtr;
#else
            byte* ptr = (byte*)Unsafe.AsPointer<byte>(ref bits[0]);
#endif
            return PrimitiveBitsToDecimal(ptr, Math.Min(_BIT_LEN128, bits.Length), cpuType);
        }

        /// <summary>
        /// 将bit位转换为整数值,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右
        /// </summary>
        /// <param name="bits">bit位数组,其中小端模式下时,结果数据索引从小到大表示二进制位依次从左到右</param>
        /// <param name="cpuType">bit位数组的字节顺序:
        /// CPUTypes.LittleEndian:小端,数据索引由0开始递增,依次为从高位到低位;
        /// false:大端,数据索引由0开始递增,依次为从低位到高位</param>
        /// <returns>转换结果</returns>
        public static decimal BitsToDecimal(SpanZ<byte> bits, ByteSequence cpuType)
        {
            if (bits.Length < 1)
            {
                throw new ArgumentOutOfRangeException("bit位数组长度不能小于1");
            }

            return PrimitiveBitsToDecimal(bits.DataPtr, Math.Min(_BIT_LEN128, bits.Length), cpuType);
        }

        private static decimal PrimitiveBitsToDecimal(byte* bitsPtr, int bits, ByteSequence cpuType)
        {
            byte[] bytes = new byte[sizeof(decimal)];
            int item;

            long index;
            if (cpuType == ByteSequence.LittleEndian)
            {
                index = bits - 1;
                for (int i = bytes.Length - 1; i >= 0; i--)
                {
                    item = 0;
                    for (int j = 0; j < 8; j++)
                    {
                        item |= (bitsPtr[index] << j);
                        index--;
                    }
                    bytes[i] = (byte)item;
                }
            }
            else
            {
                index = bits - 1;
                for (int i = 0; i < bytes.Length; i++)
                {
                    item = 0;
                    for (int j = 0; j < 8; j++)
                    {
                        item |= (bitsPtr[index] << j);
                        index--;
                    }
                    bytes[i] = (byte)item;
                }
            }

            using (var ms = new MemoryStream(bytes))
            {
                var reader = new BinaryReader(ms);
                return reader.ReadDecimal();
            }
        }

        #endregion



        /// <summary>
        /// 将bit位字符串转换为bit位数组
        /// </summary>
        /// <param name="str">bit位字符串</param>
        /// <returns>bit位数组</returns>
        /// <exception cref="ArgumentNullException">bit位字符串为null或空异常</exception>
        /// <exception cref="ArgumentException">bit位字符串中包含非法字符异常</exception>
        public static byte[] StrToBits(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                throw new ArgumentNullException(nameof(str));
            }

            byte[] bits = new byte[str.Length];
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '0')
                {
                    bits[i] = 0;
                }
                else if (str[i] == '1')
                {
                    bits[i] = 1;
                }
                else
                {
                    throw new ArgumentException($"bit位字符串中包含0和1外无效字符\"{str[i]}\"", nameof(str));
                }
            }
            return bits;
        }




        #region 循环左移

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static sbyte RotateLeft(sbyte value, int bits)
        {
            if (bits > _BIT_LEN8)
            {
                bits = bits % _BIT_LEN8;
            }

            return (sbyte)((value << bits) | ((value >> (_BIT_LEN8 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte RotateLeft(byte value, int bits)
        {
            if (bits > _BIT_LEN8)
            {
                bits = bits % _BIT_LEN8;
            }

            return (byte)((value << bits) | ((value >> (_BIT_LEN8 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static short RotateLeft(short value, int bits)
        {
            if (bits > _BIT_LEN16)
            {
                bits = bits % _BIT_LEN16;
            }

            return (short)((value << bits) | ((value >> (_BIT_LEN16 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static ushort RotateLeft(ushort value, int bits)
        {
            if (bits > _BIT_LEN16)
            {
                bits = bits % _BIT_LEN16;
            }

            return (ushort)((value << bits) | ((value >> (_BIT_LEN16 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static int RotateLeft(int value, int bits)
        {
            if (bits > _BIT_LEN32)
            {
                bits = bits % _BIT_LEN32;
            }

            return (value << bits) | ((value >> (_BIT_LEN32 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static uint RotateLeft(uint value, int bits)
        {
            if (bits > _BIT_LEN32)
            {
                bits = bits % _BIT_LEN32;
            }

            return (value << bits) | ((value >> (_BIT_LEN32 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static long RotateLeft(long value, int bits)
        {
            if (bits > _BIT_LEN64)
            {
                bits = bits % _BIT_LEN64;
            }

            return (value << bits) | ((value >> (_BIT_LEN64 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static ulong RotateLeft(ulong value, int bits)
        {
            if (bits > _BIT_LEN64)
            {
                bits = bits % _BIT_LEN64;
            }

            return (value << bits) | ((value >> (_BIT_LEN64 - bits)) | 0);
        }

#if CORE7_P

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static Int128 RotateLeft(Int128 value, int bits)
        {
            if (bits > _BIT_LEN128)
            {
                bits = bits % _BIT_LEN128;
            }

            return (value << bits) | ((value >> (_BIT_LEN128 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static UInt128 RotateLeft(UInt128 value, int bits)
        {
            if (bits > _BIT_LEN128)
            {
                bits = bits % _BIT_LEN128;
            }

            return (value << bits) | ((value >> (_BIT_LEN128 - bits)) | 0);
        }

#endif

        #endregion

        #region 循环右移

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static sbyte RotateRight(sbyte value, int bits)
        {
            if (bits > _BIT_LEN8)
            {
                bits = bits % _BIT_LEN8;
            }

            return (sbyte)((value >> bits) | ((value << (_BIT_LEN8 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static byte RotateRight(byte value, int bits)
        {
            if (bits > _BIT_LEN8)
            {
                bits = bits % _BIT_LEN8;
            }

            return (byte)((value >> bits) | ((value << (_BIT_LEN8 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static short RotateRight(short value, int bits)
        {
            if (bits > _BIT_LEN16)
            {
                bits = bits % _BIT_LEN16;
            }

            return (short)((value >> bits) | ((value << (_BIT_LEN16 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static ushort RotateRight(ushort value, int bits)
        {
            if (bits > _BIT_LEN16)
            {
                bits = bits % _BIT_LEN16;
            }

            return (ushort)((value >> bits) | ((value << (_BIT_LEN16 - bits)) | 0));
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static int RotateRight(int value, int bits)
        {
            if (bits > _BIT_LEN32)
            {
                bits = bits % _BIT_LEN32;
            }

            return (value >> bits) | ((value << (_BIT_LEN32 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static uint RotateRight(uint value, int bits)
        {
            if (bits > _BIT_LEN32)
            {
                bits = bits % _BIT_LEN32;
            }

            return (value >> bits) | ((value << (_BIT_LEN32 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static long RotateRight(long value, int bits)
        {
            if (bits > _BIT_LEN64)
            {
                bits = bits % _BIT_LEN64;
            }

            return (value >> bits) | ((value << (_BIT_LEN64 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static ulong RotateRight(ulong value, int bits)
        {
            if (bits > _BIT_LEN64)
            {
                bits = bits % _BIT_LEN64;
            }

            return (value >> bits) | ((value << (_BIT_LEN64 - bits)) | 0);
        }

#if CORE7_P

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static Int128 RotateRight(Int128 value, int bits)
        {
            if (bits > _BIT_LEN128)
            {
                bits = bits % _BIT_LEN128;
            }

            return (value >> bits) | ((value << (_BIT_LEN128 - bits)) | 0);
        }

        /// <summary>
        /// 向左循环移动指定位数
        /// </summary>
        /// <param name="value">要移位的原数据</param>
        /// <param name="bits">移动位数</param>
        /// <returns>转换结果bit位数组</returns>
        public static UInt128 RotateRight(UInt128 value, int bits)
        {
            if (bits > _BIT_LEN128)
            {
                bits = bits % _BIT_LEN128;
            }

            return (value >> bits) | ((value << (_BIT_LEN128 - bits)) | 0);
        }

#endif


        #endregion



    }
}
